/*!
 * imtask
 * 
 * Using jQuery JavaScript Library v1.7.1
 * http://jquery.com/
 *
 * Copyright 2011, Alex Imsha
 * http://imsha.ru/
 *
 * Date: 22-06-2011
 */

(function($) {
    $.fn.imtask = function(options) {
        var defaults = {
            urlhash: '#tasks',
            append: 'body',
            lang: 'en'
        };
        
        var opts = $.extend(defaults, options);
        var pluginVersion = '1.0.0';
        
        if (opts.urlhash === window.location.hash && typeof(localStorage)=='object') {
          var lang = {};
          lang.en = {
            TogglePanel: 'Toggle panel',
            Add: 'Add'
          };
          lang.ru = {
            TogglePanel: 'Спрятать/Показать',
            Add: 'Добавить'
          };
          lang.cur = lang[opts.lang];
          
          var panelStatus = localStorage.getItem('jqTasksPanel');
          if (panelStatus!=0) {
            panelStatus = 'open';
            localStorage.setItem('jqTasksPanel',1);
          } else {
            panelStatus = 'close';
            localStorage.setItem('jqTasksPanel',0);
          };
          
          var template = '';
          template += '<div id="jq-tasks" class="'+panelStatus+'">';
              template += '<div class="jq-tasks-title">';
                  template += '<span id="jq-tasks-ctrl-panel" class="jq-tasks-ctrl">'+lang.cur.TogglePanel+'</span>';
                  template += 'Tasks <span>ver. '+pluginVersion+'</span>';
              template += '</div>';
              template += '<div id="jq-tasks-list" class="jq-tasks-list"></div>';
              template += '<form id="jq-tasks-form">';
                  template += '<textarea id="jq-task-form-title"></textarea>';
                  template += '<span id="jq-task-form-submit">'+lang.cur.Add+'</span>';
              template += '</form>';
          template += '</div>';
          
  
          function setID(){
            return Math.round(+new Date()/1000);
          };
          
          function appendItem(id,title){
            var container = $('#jq-tasks-list');
            var tpl='<div class="jq-tasks-item">';
                tpl+='<input id="'+id+'" class="jq-tasks-item-check" type="checkbox" >';
                tpl+='<label for="'+id+'">'+title+'</label>';
            tpl+='</div>';
            
            $(container).append(tpl);
          };
          
          function Storage(id){
            this.id = id;
          };
          
          Storage.prototype.list = function() {
            var len = localStorage.length;
            
            for (var i=0; i<len; i++) {
              
              if (localStorage.key(i)!='jqTasksPanel') {
                var id = localStorage.key(i);
                var title = localStorage.getItem(id);
                appendItem(id,title);
              };
            };
          };
          
          Storage.prototype.add = function(title) {
            localStorage.setItem(this.id, title);
          };
          
          Storage.prototype.del = function(id) {
            localStorage.removeItem(id);
          };
          
          return this.each(function() {
              if ($('#jq-tasks').size()==0) {
                $(opts.append).append(template);
              };
              
              var SavedData = new Storage(0);
              SavedData.list();
              
              //Events
              $('#jq-task-form-submit').on('click',function(){
                var textarea = $('#jq-task-form-title');
                var id = setID();
                var BaseData = new Storage(id);
                var title = textarea.val();
                if (title.length > 0 ) {
                  textarea.val('');
                  appendItem(id,title);
                  BaseData.add(title);
                } else {
                  textarea.addClass('error');
                  textarea.bind('keyup',function(){
                    var title = textarea.val();
                    if (title.length > 0 ) {
                      textarea.removeClass('error');
                    };
                  });
                }
                return false;
              });
              
              $('.jq-tasks-item-check').on('change',function(){
                var $this = $(this);
                var id = $this.attr('id');
                var label = $('[for='+id+']');
                var text = label.text();
                var BaseData = new Storage(id);
                if($this.is(':checked')) {
                  BaseData.del(id);
                  label.addClass('jq-tasks-label-todelete');
                } else {
                  var BaseData = new Storage(id);
                  BaseData.add(text);
                  label.removeClass('jq-tasks-label-todelete');
                };
                return false;
              });
              
              $('#jq-tasks-ctrl-panel').on('click',function(){
                $('#jq-tasks-form,#jq-tasks-list').slideToggle();
                $('#jq-tasks').removeClass('close');
                var panelStatus = localStorage.getItem('jqTasksPanel');
                if (panelStatus==1) {
                  localStorage.setItem('jqTasksPanel',0);
                } else {
                  localStorage.setItem('jqTasksPanel',1);
                };
              });
          });
        };
    };
})(jQuery);



$(document).ready(function() { 
    $('body').imtask();
});